import { createRouter, createWebHistory } from 'vue-router'
import Home from '../components/Home.vue'
import About from '../components/About.vue'
import Register from '../components/Register.vue'
import Login from '../components/Login.vue'
import Vera from '../components/Vera.vue'
import Translation from '../components/Translation.vue'
import TranslationV2 from '../components/TranslationV2.vue'

const routes = [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/about',
      name: 'About',
      component: About
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/vera',
      name: 'Vera',
      component: Vera
    },
    {
      path: '/translation',
      name: 'Translation',
      component: Translation,
      meta: { requiresAuth: true } // 添加路由保护
    },
    {
      path: '/translationV2',
      name: 'TranslationV2',
      component: TranslationV2,
      meta: { requiresAuth: true } // 添加路由保护
    }
]

// 创建路由器实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加导航守卫
router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证
  if (to.meta.requiresAuth) {
    // 检查用户是否已登录（这里使用简单的本地存储检查）
    const isLoggedIn = localStorage.getItem('isLoggedIn')
    if (isLoggedIn) {
      // 用户已登录，允许访问
      next()
    } else {
      // 用户未登录，重定向到登录页面
      next('/login')
    }
  } else {
    // 路由不需要认证，直接访问
    next()
  }
})

export default router